<?php

class erpapi_logistics_matrix_pdd_request_electron extends erpapi_logistics_request_electron
{
    protected $directNum = 1;

    public function bufferRequest($sdf){
        return $this->directNum;
    }

    public function directRequest($sdf) {
        // 模板地址
        $corp = app::get('ome')->model('dly_corp')->getList('prt_tmpl_id, protect, protect_rate, minprice', array('corp_id'=>$sdf['delivery'][0]['logi_id']), 0, 1);
        $templateObj = app::get("logisticsmanager")->model('express_template');
        $printTpl = $templateObj->getList('*', array('template_id'=>$corp[0]['prt_tmpl_id']), 0, 1);
        $template = $printTpl[0];
        $templateUrl = $this->_getTemplateUrl($template);

        $this->title     = '拼多多-' . $this->__channelObj->channel['logistics_code'] . '获取电子面单';
        $this->timeOut   = 20;
        $this->primaryBn = $sdf['primary_bn'];

        // 发货地址
        $seller = array(
            'city'     => $sdf['shop']['city'],
            'detail'   => $sdf['shop']['address_detail'],
            'district' => $sdf['shop']['area'],
            'province' => $sdf['shop']['province'],
            'mobile' => $sdf['shop']['mobile'],
            'name'   => $sdf['shop']['default_sender'],
            'phone'  => $sdf['shop']['tel'],
        );

        $arrDelivery = $sdf['delivery'];
        $deliveryBnKey = array();
        $TradeOrderInfoDto = array();
        foreach($arrDelivery as $delivery) {
            $deliveryBnKey[$delivery['delivery_bn']] = $delivery;

            $arrItem = array();
            foreach($delivery['package_items'] as $pVal) {
                $arrItem[] = array(
                    'count' => $pVal['count'],
                    'name'  => $this->charFilter($pVal['item_name'])
                );
            }
            $TradeOrderInfoDto[] = array(
                'object_id'          => $delivery['delivery_bn'],
                'order_platform' => $delivery['order_channels_type'] ? $delivery['order_channels_type'] : 'OTHER',
                'tid_list'    => empty($delivery['order_bn'])
                    ? array($delivery['delivery_bn'])
                    : array_values(array_unique($delivery['order_bn'])),
                'package_id'                   => $delivery['delivery_bn'],
                'items'                => $arrItem,
                'package_volume'               => 0,
                'package_weight'               => (int)$delivery['net_weight'],
                'recipient' => array(
                    'city'     => $delivery['ship_city'],
                    'detail'   => $this->charFilter($delivery['ship_addr']),
                    'district' => $delivery['ship_district']?$delivery['ship_district']:'区',
                    'province' => $delivery['ship_province'],
                    'mobile' => $delivery['ship_mobile'],
                    'name'   => $this->charFilter($delivery['ship_name']),
                    'phone'  => $delivery['ship_tel']
                ),
                'template_url' => $templateUrl,
            );
        }

        $params = array(
            'sender'             => json_encode($seller),
            'trade_order_info_list' => json_encode($TradeOrderInfoDto),
        );

        $result = $this->requestCall(STORE_WAYBILL_PRINT, $params);

        $returnResult = $this->backToResult($result, $deliveryBnKey);

        return $returnResult;
    }

    private function _getTemplateUrl($template) {
        if(in_array($template['template_type'], array(/*'pdd_user',*/ 'pdd_standard'))
            || ($template['template_type'] == 'pdd_user' && strpos($template['template_name'], '一联单'))) {
            $url = explode(':', $template['template_data'], 2);
        } else {
            $filter = array(
                'template_type' => 'pdd_standard',
                'cp_code' => $this->__channelObj->channel['logistics_code']
            );
            $templateObj = app::get("logisticsmanager")->model('express_template');
            $printTpl = $templateObj->getList('*', $filter, 0, 1);
            if($printTpl) {
                $url = explode(':', $printTpl[0]['template_data'], 2);
            } else {
                $url[1] = 'url:http://pinduoduoimg.yangkeduo.com/print_template/2019-03-18/8d8176570314b2b7484195634f654f7c.xml';
            }
        }
        return $url[1];
    }

    private function backToResult($ret, $deliveryBnKey){
        $waybill = empty($ret['data']) ? array() : json_decode($ret['data'], true);
        if(empty($waybill)) {
            return $ret['msg'] ? $ret['msg'] : false;
        }
        $result = array();
        foreach ($waybill as $val) {
            $deliveryBn = trim($val['object_id']);
            $delivery = $deliveryBnKey[$deliveryBn];
            $printData = json_decode($val['print_data'], true);
            $position = $printData['routingInfo']['bigShotName'];
            $position_no = $printData['routingInfo']['threeSegmentCode'];
            $packageWdjc = $printData['routingInfo']['endBranchName'];
            $packageWd = $printData['routingInfo']['endBranchCode'];

            $result[] = array(
                'succ' => $val['waybill_code'] ? true : false,
                'msg' => '',
                'delivery_id' => $delivery['delivery_id'],
                'delivery_bn' => $delivery['delivery_bn'],
                'logi_no' => $val['waybill_code'],
                'mailno_barcode' => '',
                'qrcode' => '',
                'position' => trim($position),
                'position_no' => $position_no,
                'package_wdjc' => (string) $packageWdjc,
                'package_wd' => (string) $packageWd,
                'print_config' => '',
                'json_packet' => $val['print_data'],
            );
        }
        $this->directDataProcess($result);
        return $result;
    }

    public function recycleWaybill($waybillNumber) {
        app::get('logisticsmanager')->model('waybill')->update(array('status'=>2,'create_time'=>time()),array('waybill_number'=>$waybillNumber));
        $this->title = '拼多多_' . $this->__channelObj->channel['logistics_code'] . '取消电子面单';
        $this->primaryBn = $waybillNumber;
        $params = array(
            'waybill_code' => $waybillNumber
        );
        $callback = array(
            'class' => get_class($this),
            'method' => 'callback'
        );
        $this->requestCall(STORE_WAYBILL_CANCEL, $params, $callback);
    }

    public function getWaybillISearch($sdf)
    {
        $params = array();

        $title = '拼多多订购地址获取';

        $result = $this->__caller->call(STORE_HQEPAY_ORDERSERVICE,$params,array(),$title, 6, $params['cp_code']);

        if ($result['rsp'] == 'succ' && $result['data']) {
            $this->_getWISCallback($result['data']);
        }

        return array('rsp'=>$result['rsp'],'msg'=>$result['rsp']=='succ'?'获取成功':'获取失败');
    }

    private function _getWISCallback($data)
    {
        $data = json_decode($data,true);

        if (!$data) return ;

        $extendObj = app::get('logisticsmanager')->model('channel_extend');

        // 取有面单号的
        $process_params = array();
        foreach ($data[0]['branch_account_cols'] as $info) {
            if($info['quantity'] < $process_params['allocated_quantity']) {
                continue;
            }
            $process_params = array(
                'cancel_quantity'    => $info['cancel_quantity'],
                'allocated_quantity' => $info['quantity'],
                'province'           => $info['shipp_address_cols'][0]['province'],
                'city'               => $info['shipp_address_cols'][0]['city'],
                'area'               => $info['shipp_address_cols'][0]['district'],
                'address_detail'     => $info['shipp_address_cols'][0]['detail'],
                'channel_id'         => $this->__channelObj->channel['channel_id'],
                'print_quantity'     => $info['allocated_quantity'],
            );
        }

        if (!$process_params) return ;

        $extend = $extendObj->dump(array('channel_id'=>$this->__channelObj->channel['channel_id']),'id');
        if ($extend) $process_params['id'] = $extend['id'];

        $extendObj->save($process_params);
    }
}
